#!/usr/bin/env python3
"""
测试平衡视频拆分算法
"""

import asyncio
from pathlib import Path
from app.services.video_splitter import VideoSplitter

async def test_balanced_split():
    """测试平衡拆分算法"""
    
    splitter = VideoSplitter()
    
    # 测试用例1: 正常情况
    print("=== 测试用例1: 130秒视频，60秒一段，最小10秒 ===")
    segments = splitter._calculate_balanced_segments(
        total_duration=130,
        target_duration=60,
        min_duration=10,
        max_segments=20
    )
    
    print(f"分段结果: {len(segments)} 个片段")
    for i, (start, end) in enumerate(segments):
        duration = end - start
        print(f"  片段{i+1}: {start:.1f}s - {end:.1f}s (时长: {duration:.1f}s)")
    
    # 测试用例2: 会产生小片段的情况
    print("\n=== 测试用例2: 125秒视频，60秒一段，最小10秒 ===")
    segments = splitter._calculate_balanced_segments(
        total_duration=125,
        target_duration=60,
        min_duration=10,
        max_segments=20
    )
    
    print(f"分段结果: {len(segments)} 个片段")
    for i, (start, end) in enumerate(segments):
        duration = end - start
        print(f"  片段{i+1}: {start:.1f}s - {end:.1f}s (时长: {duration:.1f}s)")
    
    # 测试用例3: 极端情况 - 很短的视频
    print("\n=== 测试用例3: 45秒视频，60秒一段，最小10秒 ===")
    segments = splitter._calculate_balanced_segments(
        total_duration=45,
        target_duration=60,
        min_duration=10,
        max_segments=20
    )
    
    print(f"分段结果: {len(segments)} 个片段")
    for i, (start, end) in enumerate(segments):
        duration = end - start
        print(f"  片段{i+1}: {start:.1f}s - {end:.1f}s (时长: {duration:.1f}s)")
    
    # 测试用例4: 长视频
    print("\n=== 测试用例4: 500秒视频，60秒一段，最小10秒 ===")
    segments = splitter._calculate_balanced_segments(
        total_duration=500,
        target_duration=60,
        min_duration=10,
        max_segments=20
    )
    
    print(f"分段结果: {len(segments)} 个片段")
    for i, (start, end) in enumerate(segments):
        duration = end - start
        print(f"  片段{i+1}: {start:.1f}s - {end:.1f}s (时长: {duration:.1f}s)")
    
    # 测试用例5: 会产生多个小片段的情况
    print("\n=== 测试用例5: 185秒视频，60秒一段，最小10秒 ===")
    segments = splitter._calculate_balanced_segments(
        total_duration=185,
        target_duration=60,
        min_duration=10,
        max_segments=20
    )
    
    print(f"分段结果: {len(segments)} 个片段")
    for i, (start, end) in enumerate(segments):
        duration = end - start
        print(f"  片段{i+1}: {start:.1f}s - {end:.1f}s (时长: {duration:.1f}s)")
    
    # 验证所有片段都满足最小时长要求
    print("\n=== 验证结果 ===")
    all_valid = True
    for i, (start, end) in enumerate(segments):
        duration = end - start
        if duration < 10:
            print(f"❌ 片段{i+1}时长{duration:.1f}s小于最小要求10s")
            all_valid = False
    
    if all_valid:
        print("✅ 所有片段都满足最小时长要求")
    
    # 测试优化算法
    print("\n=== 测试优化算法 ===")
    test_segments = [
        [0, 60],    # 正常片段
        [60, 65],   # 5秒小片段
        [65, 125],  # 正常片段
        [125, 130]  # 5秒小片段
    ]
    
    print("优化前:")
    for i, seg in enumerate(test_segments):
        duration = seg[1] - seg[0]
        print(f"  片段{i+1}: {seg[0]:.1f}s - {seg[1]:.1f}s (时长: {duration:.1f}s)")
    
    optimized = splitter._optimize_small_segments(test_segments, 10, 60)
    
    print("优化后:")
    for i, seg in enumerate(optimized):
        duration = seg[1] - seg[0]
        print(f"  片段{i+1}: {seg[0]:.1f}s - {seg[1]:.1f}s (时长: {duration:.1f}s)")

if __name__ == "__main__":
    asyncio.run(test_balanced_split())